
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )


// This is the spawn function. It's called when a client calls the entity to be spawned.
// If you want to make your SENT spawnable you need one of these functions to properly create the entity
//
// ply is the name of the player that is spawning it
// tr is the trace from the player's eyes 
//
function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end
	
	local SpawnPos = tr.HitPos + tr.HitNormal * 16
	
	local ent = ents.Create( "sent_bombingbay" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	ent:SetPlayer(ply)
	
	return ent
	
end

function ENT:SetPlayer(ply)

	self.Entity:SetVar( "Founder", ply )
	self.Entity:SetPhysicsAttacker(ply)
    self.Entity:SetVar( "FounderIndex", ply:UniqueID() )
    
    self.Entity:SetNetworkedString( "FounderName", ply:Nick() )
	//print(self.Entity:GetNetworkedString( "FounderName" ))

end


/*---------------------------------------------------------
   Name: Initialize
---------------------------------------------------------*/
function ENT:Initialize()

	self.Entity:SetModel( "models/props_wasteland/tram_leverbase01.mdl" )
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS ) 
	self.Entity:SetAngles(Angle(180,0,0))
	
	self.Inputs = Wire_CreateInputs( self.Entity, { "Single Bomb", "Automatic Bombing" } )
	self.BTimer = nil //CurTime() + 3
	self.ToggledOn = false
	self.Entity.CurBomb = nil
	self.Entity.Finished = true
	
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:Wake()
		phys:SetMass(40)
		phys:EnableGravity(true)
		phys:EnableDrag(true)
		phys:EnableCollisions(true)
	end
	
end

function Stage1(self)

	local pos = self:GetPos()
	
	local s1 = "npc/sniper/reload1.wav"
	local d1 = "buttons/button8.wav"
	util.PrecacheSound(s1)
	util.PrecacheSound(d1)
	self:EmitSound(s1,100,math.random(75,85))
	self:EmitSound(d1,100,math.random(85,110))
	
	local drop = ents.Create( "sent_dropbomb" )
	if ( !drop:IsValid() ) then return end
	drop:SetPos( self:GetPos() + (self:GetUp() * 3) ) //(self.Entity:GetForward() * 50)
	drop:SetPlayer(self:GetVar( "Founder", nil ))
	drop:Spawn()
	drop:Initialize()
	drop:Activate()
	
	local ang = self.Entity:GetAngles()
	ang = ang + Angle(0,90,90)
	if ang.yaw > 360 then
		ang.yaw = ang.yaw - 360
	end
	if ang.roll > 360 then
		ang.roll = ang.roll - 360
	end
	
	drop:SetAngles( ang )
	
	drop:SetVelocity(self:GetVelocity())
	self.CurBomb = drop
	
	for k,v in pairs(ents.FindInSphere( drop:GetPos(), 100 )) do
		constraint.NoCollide( v, drop, 0, 0 ); 
	end
	
	constraint.Weld( self, drop, 0, 0, 0 )
	constraint.NoCollide( self, drop, 0, 0 ); 
	
end

function Stage2(self)

	if !self.CurBomb:IsValid() then self.Finished = true return end

	local s2 = "buttons/lever6.wav"
	util.PrecacheSound(s2)
	
	self:EmitSound(s2,100,math.random(90,115))
	
	self.CurBomb:SetPos( self:GetPos() + (self:GetUp() * 6) )
	self.CurBomb:SetAngles( self:GetAngles() + Angle(0,90,90))
	
end

function Stage3(self)

	if !self.CurBomb:IsValid() then self.Finished = true return end

	self.CurBomb:MakeActive()
	
	local s3 = "buttons/button6.wav"
	util.PrecacheSound(s3)
	self:EmitSound(s3,100,math.random(80,100))
	
	self.CurBomb:SetPos( self:GetPos() + (self:GetUp() * 10) )
	constraint.RemoveAll(self.CurBomb)
	
	self.Finished = true
	self.BTimer = nil
	self.CTimer = nil
	self.DTimer = nil
	self.CurBomb = nil
	
end


function ENT:PhysicsCollide( data, physobj ) 

  if (data.Speed > 50 && data.DeltaTime > 0.2 ) then
  self.Entity:EmitSound( Sound("physics/metal/metal_box_impact_soft"..math.random(1,3)..".wav"),100,math.random(90,120) )
  end 

end


/*---------------------------------------------------------
   Name: Think
---------------------------------------------------------*/
function ENT:Think()
	if self.ToggledOn and self.Entity.Finished == true then
		self.BTimer = CurTime() + 5
		self.CTimer = self.BTimer + 2
		self.DTimer = self.CTimer + 2
		self.Entity.Finished = false
	end
	if self.BTimer != nil then
		if (self.BTimer < CurTime()) then
			Stage1(self.Entity)
			self.BTimer = nil
		end
	elseif self.CTimer != nil then
		if (self.CTimer < CurTime()) then
			Stage2(self.Entity)
			self.CTimer = nil
		end
	elseif self.DTimer != nil then
		if (self.DTimer < CurTime()) then
			Stage3(self.Entity)
			self.DTimer = nil
		end
	end
 end 


/*---------------------------------------------------------
   Name: Use
---------------------------------------------------------*/
function ENT:Use()
	if self.Entity.Finished == false then return end
	self.BTimer = CurTime() + 5
	self.CTimer = self.BTimer + 2
	self.DTimer = self.CTimer + 2
	self.Entity.Finished = false
	
	util.PrecacheSound("buttons/lever7.wav")
	self.Entity:EmitSound("buttons/lever7.wav",100,math.random(90,120))
end

function ENT:SetTrigger( trigger )
	self.Trigger = trigger
end

function ENT:GetTrigger()
	return self.Trigger
end

/*---------------------------------------------------------
   Name: Input
---------------------------------------------------------*/
function ENT:TriggerInput(iname, value)
	if (iname == "Single Bomb") then
		if value > 0 and self.Entity.Finished == true then
			self.BTimer = CurTime() + 5
			self.CTimer = self.BTimer + 2
			self.DTimer = self.CTimer + 2
			self.Entity.Finished = false
		end
	end
	
	if (iname == "Automatic Bombing") then
		if value > 0 then
			self.ToggledOn = !self.ToggledOn
		end
	end
end